package com.mg.game.content.tasksscreen;

import android.graphics.Bitmap;

import com.mg.framework.kernel.GameApplication;
import com.mg.framework.tiled.Map;
import com.mg.game.content.ConstantString;
import com.mg.game.content.gamescreen.GameScreen;
import com.mg.game.content.gamescreen.TankManager;

public class TaskManager {

	private Task[] tasks;
	private Bitmap taskIcon;
	private int taskIconWidth;
	private int taskIconHeight;
	public int widthSpace;
	public int heightSpace;

	public TaskManager(TasksScreen screen) {
		this.taskIcon = GameApplication.me
				.loadingAssets(ConstantString.TASKICON);
		this.heightSpace = 20;
		this.taskIconHeight = (screen.height - this.heightSpace * 6) / 3;
		this.taskIconWidth = this.taskIconHeight;
		this.widthSpace = (screen.width - this.taskIconWidth * 5) / 6;
		this.tasks = new Task[ConstantString.TASKCOUNT];
		for (int i = 0; i < tasks.length; i++) {
			this.tasks[i] = new Task(this, screen, this.taskIcon, i,
					this.taskIconWidth, this.taskIconHeight, this.widthSpace,
					this.heightSpace);
		}
		screen.setTaskManager(this);
	}

	public Task[] getTask(int startIndex, int endIndex) {
		Task[] tmpTask = new Task[endIndex - startIndex + 1];
		for (int i = 0; i <= endIndex - startIndex; i++) {
			tmpTask[i] = this.tasks[i + startIndex];
		}
		return tmpTask;
	}

	private Map getTaskMap(int taskIndex) {
		return GameApplication.me.loadingTiledMapAssets(
				ConstantString.BASEPATH, ConstantString.LEVEL + taskIndex
						+ ".tmx", ConstantString.TILEOBJECTPACKAGENAME);
	}

	public void startTask(GameScreen gameScreen, int taskIndex) {
		// TODO init a game
		Map map = this.getTaskMap(taskIndex);
		gameScreen.setTaskMap(map);
		gameScreen.setTankManager(new TankManager(gameScreen,map));
	}
}